package com.qimen.controller;

import java.util.Calendar;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qimen.bean.Rili;
import com.qimen.bean.RiliExample;
import com.qimen.mapper.RiliMapper;

@RestController
public class ApiController {

	@Autowired
	private RiliMapper riliMapper;
	
	static String tiangan1[] = new String[] {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
	static String tiangan2[] = new String[] {"丙","丁","戊","己","庚","辛","壬","癸","甲","乙"};
	static String tiangan3[] = new String[] {"戊","己","庚","辛","壬","癸","甲","乙","丙","丁"};
	static String tiangan4[] = new String[] {"庚","辛","壬","癸","甲","乙","丙","丁","戊","己"};
	static String tiangan5[] = new String[] {"壬","癸","甲","乙","丙","丁","戊","己","庚","辛"};
	
	@RequestMapping("/rili")
	public Rili rili() {
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    //获取东八区时间
		int year = c.get(Calendar.YEAR);    //获取年   
		int month = c.get(Calendar.MONTH) + 1;   //获取月份，0表示1月份   
		int day = c.get(Calendar.DAY_OF_MONTH);    //获取当前天数  
		
		String gongliriqi = ""+year+"/"+month+"/"+day;
		RiliExample riliExample = new RiliExample();
		riliExample.or().andGongliriqiEqualTo(gongliriqi);
		Rili rili =  riliMapper.selectByExample(riliExample).get(0);
		String shiganzhi = getShiChen(rili.getRiganzhi().substring(0,1));
		rili.setShichen(shiganzhi);
		return rili;
	}
	/**
	 * 获取时辰
	 * @param ritiangan
	 * @return
	 */
	public String getShiChen(String ritiangan) {
		int hours = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")).get(Calendar.HOUR_OF_DAY);
		System.out.println(hours);
		String shichen = "";
		int shichenId = 0;
		if(hours >= 23 && hours < 1) {
			shichenId = 1;
			shichen = "子";
		}else if(hours < 3 && hours >= 1) {
			shichenId = 2;
			shichen = "丑";
		}else if(hours < 5 && hours >= 3) {
			shichenId = 3;
			shichen = "寅";
		}else if(hours < 7 && hours >= 5) {
			shichenId = 4;
			shichen = "卯";
		}else if(hours < 9 && hours >= 7) {
			shichenId = 5;
			shichen = "辰";
		}else if(hours < 11 && hours >= 9) {
			shichenId = 6;
			shichen = "巳";
		}else if(hours < 13 && hours >= 11) {
			shichenId = 7;
			shichen = "午";
		}else if(hours < 15 && hours >= 13) {
			shichenId = 8;
			shichen = "未";
		}else if(hours < 17 && hours >= 15) {
			shichenId = 9;
			shichen = "申";
		}else if(hours < 19 && hours >= 17) {
			shichenId = 10;
			shichen = "酉";
		}else if(hours < 21 && hours >= 19) {
			shichenId = 1;
			shichen = "戌";
		}else if(hours < 23 && hours >= 21) {
			shichenId = 2;
			shichen = "亥";
		}
		System.out.println(shichen);
		if("甲".equals(ritiangan)) {
			shichen = tiangan1[shichenId-1]+shichen;
			System.out.println(shichen);
			return shichen;
		}else if("乙".equals(ritiangan)) {
			shichen = tiangan2[shichenId-1]+shichen;
			return shichen;
		}else if("丙".equals(ritiangan)) {
			shichen = tiangan3[shichenId-1]+shichen;
			return shichen;
		}else if("丁".equals(ritiangan)) {
			shichen = tiangan4[shichenId-1]+shichen;
			return shichen;
		}else if("戊".equals(ritiangan)) {
			shichen = tiangan5[shichenId-1]+shichen;
			return shichen;
		}else if("己".equals(ritiangan)) {
			shichen = tiangan1[shichenId-1]+shichen;
			return shichen;
		}else if("庚".equals(ritiangan)) {
			shichen = tiangan2[shichenId-1]+shichen;
			return shichen;
		}else if("辛".equals(ritiangan)) {
			shichen = tiangan3[shichenId-1]+shichen;
			return shichen;
		}else if("壬".equals(ritiangan)) {
			shichen = tiangan4[shichenId-1]+shichen;
			return shichen;
		}else if("癸".equals(ritiangan)) {
			shichen = tiangan5[shichenId-1]+shichen;
			return shichen;
		}else {
			return shichen;
		}
	}
}
